﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TerrainMovement : MonoBehaviour {

	public float speed;
	public float maxspeed;
	public bool spawned;
	public bool gameRunning;
	
	
	public List<GameObject> Spawners;
	public List<GameObject> Obstacles;
	
	public void spawnObstacle ()
	{
		if(spawned == true)
		{
			int dice = Random.Range(0,10);
			if (dice > 7)
			{
				int randomObstacle = Random.Range (0,Obstacles.Count);
				int randomSpawner = Random.Range (0,Spawners.Count);
				GameObject clone = Instantiate (Obstacles[randomObstacle] ,Spawners[Random.Range(0,Spawners.Count)].transform.position, Quaternion.identity) as GameObject; 
				clone.transform.parent = Spawners[randomSpawner].transform;
			
			}
			
		
		}
		
	
	}
	
	void Start()
	{
		MainService.GameStart += BeginMovement;
	}
	
	void OnDisable ()
	{
		MainService.GameStart -= BeginMovement;
	}
	
	void BeginMovement()
	{
		gameRunning = true;
	}

	void Update () 
	{
		if (gameRunning == true )
		{
			speed = Mathf.Lerp(0,maxspeed,10);
		}
		
	
		if (spawned == true)
		{
			spawnObstacle();
			spawned = false;
		}
		
		transform.Translate(new Vector3( 0, 0, speed) * Time.deltaTime);
		
	}
}

